package com.project.service;

import com.project.dao.EmploymentInfoMapper;
import com.project.dao.ImageMapper;
import com.project.entity.EmploymentInfo;
import com.project.entity.Image;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CryFileUtil;
import com.project.utils.Base64Util;
import com.project.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * Created by hasee on 2017/4/28.
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ImagesServiceImpl extends BaseService implements ImagesService {
    @Autowired
    private CryFileUtil cryFileUtil;

    private static ImageMapper imageMapper = null;

    public void setMapper() {
        if (imageMapper == null)
            imageMapper = sqlSessionTemplate.getMapper(ImageMapper.class);//1.获取相关mapper
    }

    @Override
    public Image findById(Integer id) {
        setMapper();
        return imageMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(Image image) {
        setMapper();
        return imageMapper.updateByPrimaryKeySelective(image);
    }

    @Override
    public int add(Image image) {
        setMapper();
        return imageMapper.insertSelective(image);
    }

    @Override
    public int delete(Integer id) {
        setMapper();
        return imageMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Image> findall() {
        setMapper();
        return imageMapper.selectAll();
    }

    @Override
    public Image findByUrl(String url) {
        setMapper();
        return imageMapper.selectByUrl(url);
    }

    /**
     * author: WheelChen
     * date: 2017/7/20
     * function: 解密文件返回base64格式数据
     *
     * @param path
     * @return base64编码文件
     */
    @Override
    public String dec(String path) throws IOException {
        String ext = "data:image/" + Util.getExt(path) + ";"; // 文件格式

        String base64 = cryFileUtil.decrypted(path);

        String fullpath = ext + "base64," + base64;

        return fullpath;
    }
}
